﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Trippy.Levels;

namespace Trippy.Animation
{
     class EffectAnimation : DrawableTrippyComponent
     {
        public enum Effects { Pow, Explosion, SmokeJump, SmokeBomb, Custom,
            IanCatFollow
        } //stock effects that get used alot;
        public Texture2D SpriteSheet { get; protected set; }

        public static Texture2D PowSprite;
        public static Texture2D ExplSprite;
        public static Texture2D CatFollow;
        public static Texture2D SmJuSprite;
        public static Texture2D SmBoSprite;

        protected float activeTime;
        int loopCount = 1;

        public delegate void FinishHandler(); //called when animation finishes.

        protected FinishHandler fhandler;

        public EffectAnimation(Level level, 
            Texture2D spriteSheet, int spriteHeight, int spriteWidth, int verticalBorder, int horizontalBorder, int verticalTrim, int horizontalTrim, int numFrames, float animationSpeed, 
            int loops, Vector2 pos) :
            base(level.Game)
        {
            CurrentLevel = level;
            Position = pos;
            loopCount = loops;
            Sprite = new AnimatedSprite(this, spriteSheet, spriteHeight, spriteWidth, verticalBorder, horizontalBorder, verticalTrim, horizontalTrim, numFrames, animationSpeed);
            activeTime = (float)Sprite.FrameCount * (float)loops;
            CurrentLevel.Game.Components.Add(this);
        }

         public EffectAnimation(Level level, 
            Texture2D spriteSheet, int spriteHeight, int spriteWidth, int verticalBorder, int horizontalBorder, int verticalTrim, int horizontalTrim, int numFrames, float animationSpeed, 
            int loops, Vector2 pos, FinishHandler fh) :
            this(level, 
            spriteSheet,spriteHeight, spriteWidth, verticalBorder, horizontalBorder, verticalTrim, horizontalTrim, numFrames, animationSpeed, 
            loops, pos)
         {
             fhandler = fh;
         }

        public EffectAnimation(Level level, Effects e, Vector2 pos) :
            base(level.Game)
        {
            CurrentLevel = level;
            Position = pos;
            switch (e)
            {
                case Effects.Pow:
                    if (PowSprite == null)
                        PowSprite = level.Game.Content.Load<Texture2D>("Sprites/Effects/pow");
                    SpriteSheet = PowSprite;
                    Sprite = new AnimatedSprite(this,
                        SpriteSheet,
                        75, 75, 0, 0, 0, 0, 4, 0.1f);
                    break;
                case Effects.Explosion:
                    if (ExplSprite == null)
                        ExplSprite = level.Game.Content.Load<Texture2D>("Sprites/Effects/explosion");
                    SpriteSheet = ExplSprite;
                    Sprite = new AnimatedSprite(this,
                        SpriteSheet,
                        64, 64, 0, 0, 0, 0, 24, 1f);
                    break;
                case Effects.IanCatFollow:
                    if (CatFollow == null)
                        CatFollow = level.Game.Content.Load<Texture2D>("Sprites/Effects/iancatfollow");
                    SpriteSheet = CatFollow;
                    Sprite = new AnimatedSprite(this,
                        SpriteSheet,
                        64, 64, 0, 0, 0, 0, 24, 1f);
                    break;
                case Effects.SmokeJump:
                    if (SmJuSprite == null)
                        SmJuSprite = level.Game.Content.Load<Texture2D>("Sprites/Effects/smokejump");
                    SpriteSheet = SmJuSprite;
                    Sprite = new AnimatedSprite(this,
                        SpriteSheet,
                        64, 64, 0, 0, 0, 0, 25, 0.5f);
                    break;
                case Effects.SmokeBomb:
                    if (SmBoSprite == null)
                        SmBoSprite = level.Game.Content.Load<Texture2D>("Sprites/Effects/smokebomb");
                    SpriteSheet = SmBoSprite;
                    Sprite = new AnimatedSprite(this,
                        SpriteSheet,
                        64, 64, 0, 0, 0, 0, 25, 0.5f);
                    break;
                default:
                    throw new NotImplementedException();
            }
            activeTime = (float)Sprite.FrameCount;
            CurrentLevel.Game.Components.Add(this);
        }

        public EffectAnimation(Level level, Effects e, Vector2 pos, FinishHandler fh) :
            this(level, e, pos)
        {
            fhandler = fh;
        }

        bool attached = false;
        DrawableTrippyComponent component;

        public void AttachToComponent(DrawableTrippyComponent x)
        {
            attached = true;
            component = x;
        }

         public void StopAnimation()
         {
             MarkForDeletion();
            if (fhandler != null)
                fhandler();
         }

        public override void Update(GameTime gameTime)
        {
            if (attached)
                Position = component.Position;
            if (!markedForDeletion)
            {
                Sprite.Step();
                activeTime -= Sprite.AnimationSpeed;
                if (activeTime <= 0)
                {
                    loopCount--;
                    if (loopCount <= 0)
                    {
                        CurrentLevel.Game.Components.Remove(this);
                        if (fhandler != null)
                            fhandler();
                    }
                    else
                        activeTime = Sprite.FrameCount;
                }
            }
            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            if(!markedForDeletion)
                StandardDraw(gameTime);
            base.Draw(gameTime);
        }

     }
}
